package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * @author: ggm
 * @data 2024年11月06日 19:05
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author: ggm
     * @date: 2024/11/6
     * 菜品分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);//后绪步骤定义
        return Result.success(pageResult);
    }

    /**
     * @Author: ggm
     * @date: 2024/11/6
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);//后绪步骤实现
        return Result.success(dishVO);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/6
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品:{}", categoryId);
        List<Dish> dishList = dishService.getByCategoryId(categoryId);
        return Result.success(dishList);

    }

    /**
     * @Author: ggm
     * @date: 2024/11/8
     * 添加菜品
     */
    @PostMapping
    public Result insertDish(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品:{}", dishDTO);
        dishService.insertDish(dishDTO);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();

    }

    /**
     * @Author: ggm
     * @date: 2024/11/11
     * 批量删除菜品
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        //1,打印日志
        log.info("菜品批量查询:{}", ids);
        //2,调用service层
        dishService.delete(ids);
        //删除菜品后即使清理缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        //3,返回结果
        return Result.success();
    }

    /**
     * @Author: ggm
     * @date: 2024/11/11
     * 修改菜品信息
     */
    @PutMapping
    public Result dishUpdate(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}", dishDTO);
        dishService.updateDish(dishDTO);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();

    }
     /**
      * @Author: ggm
      * @date:  2024/12/10
      * 起售停售菜品
      */
     @PostMapping("/status/{status}")
     public Result<String > startOrStop(@PathVariable Integer status, Long id) {
         log.info("起售停售菜品:{},{}", status, id);
         dishService.startOrStop(status,id);
         //清理缓存
         Set keys = redisTemplate.keys("dish_*");
         redisTemplate.delete(keys);
         return Result.success();

     }



}
